﻿using System;
using System.Reflection;
using SmartMotion.Core.Tools;
using Sunny.UI;

namespace SmartMotion.Core
{
    public class PropertyBase : ActionItem
    {
        protected PropertyInfo _propertyInfo;

        public override bool Initilize()
        {
            try
            {
                string fileName = Environment.CurrentDirectory + "\\" + Dll;
                var curAssembly = Assembly.LoadFile(fileName);
                var type = curAssembly.GetType(TypeName);

                _propertyInfo = type.GetProperty(MethodName);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Em.GetEvent<DebugMessage>().Publish($"初始化{ItemName}时出现异常:{e.Message}");
                return false;
            }
            return true;
        }

        public override UIForm GetConfigForm()
        {
            return new FrmSelectProperty(this);
        }
    }

    public class FieldBase : ActionItem
    {
        protected FieldInfo _fieldInfo;

        public override bool Initilize()    
        {
            try
            {
                string fileName = Environment.CurrentDirectory + "\\" + Dll;
                var curAssembly = Assembly.LoadFile(fileName);
                var type = curAssembly.GetType(TypeName);

                _fieldInfo = type.GetField(MethodName);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Em.GetEvent<DebugMessage>().Publish($"初始化{ItemName}时出现异常:{e.Message}");
                return false;
            }
            return true;
        }

        public override UIForm GetConfigForm()
        {
            return new FrmSelectField(this);
        }
    }
}
